//# sourceURL=http://uMapApp/Module/InspectionModule/SampledPositionViewport
define([
    "mhjs/mhjs",
    "uMap/uMap",
    "uEarth/uEarth",
    "../../Earth/ViewPort"
], function (
    mhjs,
    uMap,
    uEarth,
    ViewPort
) {

    function SampledPositionViewport(option) {
        var _this = this;

        _this._direction = 1;
        _this._speed = 1;
        _this._height = 2;
        _this.systimeRate = 0.02;
        _this._pitch = 20;
        _this._pathHeightAdjust = 15;
        // _this.range = _this._height / Math.sin(uEarth.Math.toRadians(_this._pitch));
        _this.range = _this._height;

        _this.clock = new uEarth.Clock({
            clockRange: uEarth.ClockRange.CLAMPED,
            clockStep : uEarth.ClockStep.TICK_DEPENDENT,
            multiplier: _this.direction * _this.speed * _this.systimeRate
        });

        _this.onTickListener = function () {
            _this.clock.tick();
            _this.updateCamera();
            _this.trigger("tick");
        };

        option && _this.OptionToProperties(option);
    }

    var _class = SampledPositionViewport;

    mhjs.OptionToPropertiesInterface(_class);
    mhjs.EventInterface(_class);

    Object.defineProperties(_class.prototype, {
        "play"                      : {
            value: function () {
                var _this = this;
                _this.clock.shouldAnimate = true;
                _this.app.earth.clock.onTick.addEventListener(_this.onTickListener);
                _this.isPlaying = true;
                _this.trigger("play");
                _this.trigger("toggle");
            }
        },
        "pause"                     : {
            value: function () {
                var _this = this;
                _this.app.earth.clock.onTick.removeEventListener(_this.onTickListener);
                _this.clock.shouldAnimate = false;
                _this.app.earth.camera.lookAtTransform(uEarth.Matrix4.IDENTITY);
                _this.isPlaying = false;
                _this.trigger("pause");
                _this.trigger("toggle");
            }
        },
        "toggle"                    : {
            value: function () {
                var _this = this;
                if (_this.isPlaying) {
                    _this.pause();
                } else {
                    _this.play();
                }
            }
        },
        "backward"                  : {
            value: function () {
                var _this = this;
                if (_this.direction === 1) {
                    _this.clock.currentTime = _this.clock.startTime.clone();
                } else {
                    _this.clock.currentTime = _this.clock.stopTime.clone();
                }
            }
        },
        "forward"                   : {
            value: function () {
                var _this = this;
                if (_this.direction === -1) {
                    _this.clock.currentTime = _this.clock.startTime.clone();
                } else {
                    _this.clock.currentTime = _this.clock.stopTime.clone();
                }

            }
        },
        "direction"                 : {
            set: function (direction) {
                var _this = this;

                direction = Number(direction);

                _this._direction = direction;
                _this.clock.multiplier = _this.direction * _this.speed * _this.systimeRate;

            },
            get: function () {
                return this._direction;
            }
        },
        "pitch"                     : {
            set: function (pitch) {
                var _this = this;

                pitch = Number(pitch);

                _this._pitch = pitch;
                // _this.range = _this.height / Math.sin(uEarth.Math.toRadians(_this.pitch));

            },
            get: function () {
                return this._pitch;
            }
        },
        "height"                    : {
            set: function (height) {
                var _this = this;

                height = Number(height);

                _this._height = height;
                // _this.range = _this.height / Math.sin(uEarth.Math.toRadians(_this.pitch));
                _this.range = _this._height;
            },
            get: function () {
                return this._height;
            }
        },
        "speed"                     : {
            set: function (speed) {
                var _this = this;

                speed = Number(speed);

                _this._speed = speed;
                _this.clock.multiplier = _this.direction * _this.speed * _this.systimeRate;
            },
            get: function () {
                return this._speed;
            }
        },
        "sampledPosition"           : {
            set: function (sampledPosition) {
                var _this = this;
                _this._sampledPosition = sampledPosition;

                //改变路径插值类型，平滑动画>>和类型无关

                // sampledPosition.forwardExtrapolationType = uEarth.ExtrapolationType.EXTRAPOLATE;
                // sampledPosition.setInterpolationOptions({
                //     interpolationDegree: 5,
                //     interpolationAlgorithm: uEarth.LinearApproximation
                // });

                sampledPosition.setInterpolationOptions({
                    interpolationDegree   : 5,
                    interpolationAlgorithm: uEarth.LagrangePolynomialApproximation
                });

                //减少节点，平滑动画>>和点数无关

                // var reduceRate = 5;
                // var toRemoveTimes = [];
                //
                // console.log(sampledPosition._property._times.length);
                //
                // $.each(sampledPosition._property._times, function (i, time) {
                //     if (i !== 0 && i !== (sampledPosition._property._times.length - 1) && (i % reduceRate !== 0)) {
                //         toRemoveTimes.push(time);
                //     }
                // });
                //
                // $.each(toRemoveTimes, function (i, time) {
                //     sampledPosition.removeSample(time);
                // });
                //
                // console.log(sampledPosition._property._times.length);

                _this.clock.startTime = sampledPosition._property._times[0];
                _this.clock.stopTime = sampledPosition._property._times[sampledPosition._property._times.length - 1];
                _this.clock.currentTime = sampledPosition._property._times[0].clone();
            },
            get: function () {
                return this._sampledPosition;
            }
        },
        "updateCamera"              : {
            value: function () {

                var _this = this;

                var position = new uMap.Position(_this.sampledPosition.getValue(_this.clock.currentTime));
                position.height = _this._pathHeightAdjust;

                _this.app.earth.camera.viewBoundingSphere(
                    new uEarth.BoundingSphere(position.cartesian, 1),
                    new uEarth.HeadingPitchRange(
                        uEarth.Math.toRadians(_this.getSampledPositionsHeading() + (_this.direction === 1 ? 0 : 180)), // east, default value is 0.0 (north)
                        uEarth.Math.toRadians(-_this.pitch),
                        _this.range
                    )
                );
            }
        },
        "updateCameraNow"           : {
            value: function () {
                var _this = this;
                if (!_this.isPlaying) {
                    _this.updateCamera();
                    _this.app.earth.camera.lookAtTransform(uEarth.Matrix4.IDENTITY);
                }
            }
        },
        "getSampledPositionsHeading": {
            value: function () {

                var _this = this;

                var heading;

                var currentPosition = _this.sampledPosition.getValue(_this.clock.currentTime);
                var previousPosition = _this.sampledPosition.getValue(uEarth.JulianDate.addSeconds(_this.clock.currentTime, -1, new uEarth.JulianDate()));
                var nextPosition = _this.sampledPosition.getValue(uEarth.JulianDate.addSeconds(_this.clock.currentTime, 1, new uEarth.JulianDate()));

                if (previousPosition || nextPosition) {

                    if (previousPosition) {
                        heading = uMap.Positions.getHeading(previousPosition, currentPosition);
                    } else {
                        heading = uMap.Positions.getHeading(currentPosition, nextPosition);
                    }

                }

                return heading;
            }
        }
    });

    return _class;

});